package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.DriverOilSonCardBiz;
import com.egao.orderB.entity.DriverOilSonCard;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-07-07 16:54:58
 * @version ：1.0
 */
@Repository
public class DriverOilSonCardBizImpl extends BaseDao implements DriverOilSonCardBiz {

	@Override
	public JsonResult list(PageResult2<T> page, DriverOilSonCard driverOilSonCard, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<DriverOilSonCard> rm = new BeanPropertyRowMapper<DriverOilSonCard>(DriverOilSonCard.class);
		StringBuffer sql = new StringBuffer("select d.id,d.main_card_id,d.organization_id,d.driver_id");
        sql.append(",d.driver,d.car_no,d.oil_card,d.price");
		sql.append(",d.oil,d.remark,d.create_time,d.create_user");
		sql.append(",d.update_time,d.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_driver_oil_son_card d ");
        sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
        sql.append(" where d.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
			sql.append(" and d.organization_id=:tableOrganizationId ");
			map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
		}
		if(Utils.isNotEmpty(driverOilSonCard)){
			if(Utils.isNotEmpty(driverOilSonCard.getMainCardId())){
				sql.append(" and instr(d.main_card_id,:main_card_id)>0 ");
				map.put("main_card_id", driverOilSonCard.getMainCardId()+"");
			}
			if(Utils.isNotEmpty(driverOilSonCard.getSort())&&Utils.isNotEmpty(driverOilSonCard.getOrder())){
					sql.append(" order by d."+driverOilSonCard.getSort()+" "+driverOilSonCard.getOrder());
			} else {
					sql.append(" order by d.create_time desc");
			}
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<DriverOilSonCard> list(DriverOilSonCard driverOilSonCard, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<DriverOilSonCard> rm = new BeanPropertyRowMapper<DriverOilSonCard>(DriverOilSonCard.class);
        StringBuffer sql = new StringBuffer("select d.id,d.main_card_id,d.organization_id,d.driver_id");
        sql.append(",d.driver,d.car_no,d.oil_card,d.price");
		sql.append(",d.oil,d.remark,d.create_time,d.create_user");
		sql.append(",d.update_time,d.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_driver_oil_son_card d ");
        sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
        sql.append(" where d.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and d.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(driverOilSonCard)){
				if(Utils.isNotEmpty(driverOilSonCard.getMainCardId())){
					sql.append(" and instr(d.main_card_id,:main_card_id)>0 ");
					map.put("main_card_id", driverOilSonCard.getMainCardId()+"");
				}
            if(Utils.isNotEmpty(driverOilSonCard.getSort())&&Utils.isNotEmpty(driverOilSonCard.getOrder())){
				sql.append(" order by d."+driverOilSonCard.getSort()+" "+driverOilSonCard.getOrder());
			} else {
				sql.append(" order by d.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public DriverOilSonCard getById(DriverOilSonCard driverOilSonCard) {
		RowMapper<DriverOilSonCard> rm = new BeanPropertyRowMapper<DriverOilSonCard>(DriverOilSonCard.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select d.id,d.main_card_id,d.organization_id,d.driver_id");
        sql.append(",d.driver,d.car_no,d.oil_card,d.price");
		sql.append(",d.oil,d.remark,d.create_time,d.create_user");
		sql.append(",d.update_time,d.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_driver_oil_son_card d ");
        sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
        sql.append(" where d.deleted=0 and d.id=:id ");
		map.put("id", driverOilSonCard.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(DriverOilSonCard driverOilSonCard, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_driver_oil_son_card (");
        sql.append("main_card_id,organization_id,driver_id,driver,car_no");
		sql.append(",oil_card,price,oil,remark");
		sql.append(",create_user,create_time) values ( ");
        sql.append(":main_card_id,:organization_id,:driver_id,:driver,:car_no");
		sql.append(",:oil_card,:price,:oil,:remark");
		sql.append(",:create_user,now())");
        map.put("main_card_id", driverOilSonCard.getMainCardId()+"");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("driver_id", driverOilSonCard.getDriverId()==null?null:driverOilSonCard.getDriverId()+"");
        map.put("driver", driverOilSonCard.getDriver());
        map.put("car_no", driverOilSonCard.getCarNo());
        map.put("oil_card", driverOilSonCard.getOilCard());
        map.put("price", "0");
        map.put("oil", "0");
        map.put("remark", driverOilSonCard.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(DriverOilSonCard driverOilSonCard, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_driver_oil_son_card set ");
        sql.append("driver_id=:driver_id,driver=:driver,car_no=:car_no ");
		sql.append(",oil_card=:oil_card ");
		sql.append(",remark=:remark,update_time=now(),update_user=:update_user where id=:id ");
        map.put("driver_id", driverOilSonCard.getDriverId()==null?null:driverOilSonCard.getDriverId()+"");
        map.put("driver", driverOilSonCard.getDriver());
        map.put("car_no", driverOilSonCard.getCarNo());
        map.put("oil_card", driverOilSonCard.getOilCard());
        map.put("remark", driverOilSonCard.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", driverOilSonCard.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}
	@Override
	public boolean distribution(DriverOilSonCard driverOilSonCard, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_driver_oil_son_card set ");
		sql.append(" update_time=now()  ");
		if(Utils.isNotEmpty(driverOilSonCard)){
			if(Utils.isNotEmpty(driverOilSonCard.getPrice())){
				sql.append(" ,price=price+:price ");
				map.put("price", driverOilSonCard.getPrice());
			}
			if(Utils.isNotEmpty(driverOilSonCard.getOil())){
				sql.append(" ,oil=oil+:oil ");
				map.put("oil", driverOilSonCard.getOil());
			}
		}
		sql.append(",update_user=:update_user where id=:id ");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", driverOilSonCard.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean deduction(DriverOilSonCard driverOilSonCard, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_driver_oil_son_card set ");
		sql.append(" update_time=now()  ");
		if(Utils.isNotEmpty(driverOilSonCard)){
			if(Utils.isNotEmpty(driverOilSonCard.getPrice())){
				sql.append(" ,price=price-:price ");
				map.put("price", driverOilSonCard.getPrice());
			}
			if(Utils.isNotEmpty(driverOilSonCard.getOil())){
				sql.append(" ,oil=oil-:oil ");
				map.put("oil", driverOilSonCard.getOil());
			}
		}
		sql.append(",update_user=:update_user where id=:id ");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", driverOilSonCard.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(DriverOilSonCard driverOilSonCard, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(driverOilSonCard.getId().length()>0){
			for(String ID : driverOilSonCard.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_driver_oil_son_card set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
